﻿using ContributionDeposit.CoreFrontEnd;
using Models.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using PagedList;
namespace ContributionDeposit.Controllers
{
    public class CountryController : Core_FrontEnd
    {
        string code = string.Empty;
        //
        // GET: /Country/
        public ActionResult Index(int? page, string searchtext = "")
        {
            ViewBag.SearchText = searchtext;
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(fun_db.GetListCountry(searchtext).ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /Country/Details/5
        public ActionResult Details(Guid id)
        {
            return View(fun_db.GetCountryById(id));
        }
        #region Create Country
        //
        // GET: /Country/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Country/Create
        [HttpPost]
        public ActionResult Create(Country country)
        {
            try
            {
                bool result = fun_db.CheckCodeCountry(country.Code);
                if (!result)
                {
                    ViewBag.Check = "Country code already exists in the database";
                    return View();
                }
                fun_db.AddNewCountry(country);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion
       
        //
        // GET: /Country/Edit/5
        public ActionResult Edit(Guid id)
        {
            Country ct = fun_db.GetCountryById(id);
            ViewBag.Code = ct.Code;
            return View(ct);
        }
        //
        // POST: /Country/Edit/5
        [HttpPost]
        public ActionResult Edit(Country country, string code)
        {
            try
            {
                bool result = fun_db.CheckCodeCountry(country.Code);

                if (country.Code != code && !result)
                {
                    ViewBag.Check = "Country code already exists in the database";
                    return View();
                }
                fun_db.EditCountry(country);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public async Task<ActionResult> Delete(Guid id)
        {
            try
            {
                if (await fun_db.DeleteCountry(id) == 1)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

        public async Task<ActionResult> DeleteSelected(IEnumerable<Guid> countryItemDelete)
        {
            try
            {
                fun_db.DeleteSelectedCountry(countryItemDelete);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }

        }

    }
}
